﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace InteractingSquares
{
  public partial class MainPage : UserControl, INotifyPropertyChanged
  {
    Storyboard m_AnimationLoop;
    ObservableCollection<Square> m_Squares;
    int m_LoopCounter;
    public event PropertyChangedEventHandler PropertyChanged;
    private double m_CanvasHeight;
    private double m_CanvasWidth;
    private Square m_SelectedSquare;
    private Point m_MousePos;

    public MainPage()
    {
      InitializeComponent();
      SquarePlayground.MouseMove += Mouse_Moved;
      ConfigPanel.DataContext = this;
      this.SizeChanged += new SizeChangedEventHandler(MainPage_SizeChanged);
      InitAnimationLoop();
      m_Squares = new ObservableCollection<Square>();
      AddSquare();
    }

    private void Mouse_Moved(object sender, MouseEventArgs e)
    {
      MousePos = e.GetPosition(SquarePlayground);
    }

    public Point MousePos
    {
      get
      {
        return m_MousePos;
      }
      set
      {
        m_MousePos = value;
        NotifyPropertyChanged("MousePos");
      }
    }

    void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
    {
      CanvasHeight = SquarePlayground.ActualHeight;
      CanvasWidth = SquarePlayground.ActualWidth;
    }

    public Square SelectedSquare
    {
      get
      {
        return m_SelectedSquare;
      }
      set
      {
        m_SelectedSquare = value;
        NotifyPropertyChanged("SelectedSquare");
      }
    }

    public double CanvasHeight
    {
      get
      {
        return this.m_CanvasHeight;
      }
      set
      {
        this.m_CanvasHeight = value;
        NotifyPropertyChanged("CanvasHeight");
      }
    }

    public double CanvasWidth
    {
      get
      {
        return this.m_CanvasWidth;
      }
      set
      {
        this.m_CanvasWidth = value;
        NotifyPropertyChanged("CanvasWidth");
      }
    }

    public ObservableCollection<Square> Squares
    {
      get
      {
        return m_Squares;
      }
      set
      {
        m_Squares = value;
      }
    }

    private void InitAnimationLoop()
    {
      m_LoopCounter = 0;
      m_AnimationLoop = new Storyboard();
      m_AnimationLoop.Duration = TimeSpan.FromMilliseconds(10);
      m_AnimationLoop.Completed += m_AnimationLoop_Completed;
      m_AnimationLoop.Begin();
    }

    private void AddSquare()
    {
      Square sq = new Square(new Point { X = 15, Y = 15 }, 30);
      sq.Direction = 0;
      sq.Speed = 10;
      SquarePlayground.Children.Add(sq);
      Squares.Add(sq);
      sq.MouseLeftButtonDown += Square_Selected;
    }

    private void Square_Selected(object sender, MouseButtonEventArgs e)
    {
      SelectedSquare = sender as Square;
    }

    private void m_AnimationLoop_Completed(object sender, EventArgs e)
    {
      Move();
      UpdateLayout();
      LoopManagement();
    }

    private void Move()
    {
      foreach (Square Square in Squares)
      {
        Square.Move();
      }
    }

    private void UpdateLayout()
    {

    }

    private void LoopManagement()
    {
      if (m_LoopCounter == 10000)
        m_LoopCounter = 0;
      m_LoopCounter++;
      m_AnimationLoop.Begin();
    }

    public void NotifyPropertyChanged(string propertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this,
            new PropertyChangedEventArgs(propertyName));
      }
    }

  }
}
